import { JXDef } from "../../../conventions/JXCommon";
import { GCtrl } from "../../../Core/GCtrl";
import GPage from "../../../Core/GView/GPage";
import MathEx from "../../../Core/Math/MathEx";
import { CMsg, CURRENCY, ITEM_TYPE, PointInfo, REWARD, SQUAD_BUTTON } from "../../Common/Define";
import { L } from "../../Common/Language";
import { VIEW_ID } from "../../Common/UI";
import { JXLocales } from "../../Common/Zh";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";


const { ccclass, property, menu } = cc._decorator;
@ccclass @menu('View/Home/RecruitCtrl')
export default class RecruitCtrl extends GPage {
    @property({ type: cc.Label, tooltip: "招募次数" }) recruit: cc.Label = null;
    @property({ type: cc.Sprite, tooltip: "招募进度" }) recruitProgress: cc.Sprite = null;
    @property({ type: cc.Node, tooltip: "招募宝箱光" }) recruiLight: cc.Node = null;
    onGLoad() {
        console.log('GameMgr.lUserData.recruitCtrl', GameMgr.lUserData.recruitCtrl);
        if (GameMgr.lUserData.recruitCtrl >= GameMgr.addDialData.getRaw<SAddDialDataRaw>(GameMgr.lUserData.addDial).needNext) {
            GameMgr.lUserData.addDial = 1;
        }
        let num = GameMgr.lUserData.receiveId;
        if (GameMgr.lUserData.addDial > num) {
            this.recruiLight.active = true;
        } else {
            this.recruiLight.active = false;
        }

        this.recruit.string = GameMgr.lUserData.recruitCtrl + '/' + GameMgr.addDialData.getRaw<SAddDialDataRaw>(GameMgr.lUserData.addDial).needNext;
    }

    onPageIn() {
        GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.recruit, { msg: PointInfo.rcruit_enter })
    }

    /**单次招募 */
    public onceRecruit() {
        let onceCard = GameMgr.systemConfig.value<number>(JXDef.SYS_CONFIG_KEY.onceCard);
        let coin = new RJXItem([CURRENCY.GOLD, ITEM_TYPE.CURRENCY, onceCard])
        let result = GameMgr.itemMgr.testCost(coin, 1, true)
        if (result.enough) {
            GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, [[CURRENCY.GOLD, ITEM_TYPE.CURRENCY, -onceCard]])
            this.luckDraw(1);
            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.recruit, { msg: PointInfo.recruit_click })
        } else {
            GameMgr.uiMgr.showToast(result.tip);
        }
    }

    /**10次招募 */
    public tenRecruit() {
        GameMgr.sdkMgr.watchAd(() => {
            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.recruit, { msg: PointInfo.recruit_ad })
            this.luckDraw(10);
        })
    }

    /**领取宝箱 */
    public boxRecruit() {
        let num = GameMgr.lUserData.receiveId;
        if (GameMgr.lUserData.addDial > num) {
            GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, [GameMgr.addDialData.getRaw<SAddDialDataRaw>(num).award]);
            GameMgr.sdkMgr.umaSetPoint(JXDef.umaPoint.recruit, { msg: L(JXLocales.tower.clickRecruit, GameMgr.addDialData.getRaw<SAddDialDataRaw>(num).needNext) })
            GameMgr.lUserData.receiveId = 1;
            num = GameMgr.lUserData.receiveId;
            if (GameMgr.lUserData.addDial > num) {
                this.recruiLight.active = true;
            } else {
                this.recruiLight.active = false;
            }
        } else {
            this.recruiLight.active = false;
        }
    }

    //抽奖奖池
    protected luckDraw(boxType: number = 1) {
        GameMgr.lUserData.recruitCtrl = boxType;
        let drawData = null;
        drawData = GameMgr.callData.data.values();
        //抽奖奖池
        let drawList = [];
        let array = [];
        drawData.forEach((value: SCallDataRaw) => {
            let config: recruitDrawConfig = {
                id: value.id,
                weight: value.weight,
                award: value.award,
            }
            drawList.push(config);
        })

        for (let i = 0; i < drawList.length; i++) {
            let weight = drawList[i].weight;
            for (let j = 0; j < weight; j++) {
                array.push(i);
            }
        }
        array = MathEx.fisherYatesShuffle(array);
        if (boxType == 1) {
            let index = Math.floor(Math.random() * array.length);
            let mainArray: Array<Array<number>> = [];
            mainArray.push(drawList[array[index]].award);
            let recruitCardCtrl: recruitCardCtrl = {
                recruitNum: boxType,
                award: mainArray,
                cb: () => {
                    GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, mainArray);
                }
            }
            GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.recuitCardCtrl, recruitCardCtrl);
            drawList = [];
        } else if (boxType == 10) {
            let mainArray: Array<Array<number>> = [];
            let isHero: boolean = false;
            let allHero = GameMgr.rHeroData.getHaveHeros(SQUAD_BUTTON.ALL);
            let num = allHero.have.length + allHero.notHave.length;
            for (let i = 0; i < boxType; i++) {
                let index = Math.floor(Math.random() * array.length);
                if (drawList[array[index]].award[1] == 0) {
                    isHero = true;
                }
                mainArray.push(drawList[array[index]].award);
            }
            if (!isHero) {
                mainArray.splice(Math.floor(Math.random() * mainArray.length), 1);
                let arr = [(1001 + (Math.floor(Math.random() * num))), 0, 1];
                mainArray.push(arr);
                console.log("十连抽没得到英雄补进", arr);
            }
            // console.log(drawList, 'drawList[array[index]', drawList[array[index]]);

            let recruitCardCtrl: recruitCardCtrl = {
                recruitNum: boxType,
                award: mainArray,
                cb: () => {
                    GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, mainArray, REWARD.HERO);
                    mainArray = [];
                }
            }
            GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.recuitCardCtrl, recruitCardCtrl);
            // GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.rewardCtrl, mainArray);
            drawList = [];

        }
        if (GameMgr.lUserData.recruitCtrl >= GameMgr.addDialData.getRaw<SAddDialDataRaw>(GameMgr.lUserData.addDial).needNext) {
            GameMgr.lUserData.addDial = 1;
        }
        this.recruitProgress.fillRange = -(GameMgr.lUserData.recruitCtrl / GameMgr.addDialData.getRaw<SAddDialDataRaw>(GameMgr.lUserData.addDial).needNext);
        this.recruit.string = GameMgr.lUserData.recruitCtrl + '/' + GameMgr.addDialData.getRaw<SAddDialDataRaw>(GameMgr.lUserData.addDial).needNext;
        let num = GameMgr.lUserData.receiveId;
        if (GameMgr.lUserData.addDial > num) {
            this.recruiLight.active = true;
        } else {
            this.recruiLight.active = false;
        }
    }
}
